<template>
  <div class="article-manager">
    <div class="i-layout-page-header header_top">
      <div class="i-layout-page-header fl_header">
        <router-link :to="{ path: '/admin/basic/information/building' }">
          <Button icon="ios-arrow-back" size="small" type="text">返回</Button>
        </router-link>
        <Divider type="vertical" />
        <span
          class="ivu-page-header-title mr20"
          style="padding: 0"
          v-text="$route.params.id ? '编辑楼宇' : '新建楼宇'"
        ></span>
      </div>
    </div>
    <Card :bordered="false" dis-hover class="ivu-mt">
      <Form
        class="form"
        ref="formValidate"
        :model="formValidate"
        :rules="ruleValidate"
        :label-width="labelWidth"
        :label-position="labelPosition"
        @submit.native.prevent
      >
        <div class="goodsTitle acea-row">
          <div class="title">基础信息</div>
        </div>
        <Row :gutter="24" type="flex">
          <Col span="24" class="mr50">
            <FormItem label="所属街道：" prop="jiedaoName" label-for="jiedaoName">
              <Input v-model="formValidate.jiedaoName" placeholder="请输入所属街道" element-id="jiedaoName"
                     style="width: 70%" />
            </FormItem>
          </Col>
          <Col span="24" class="mr50">
            <FormItem label="社区名称：" prop="sqmc" label-for="sqmc">
              <Input v-model="formValidate.sqmc" placeholder="请输入社区名称" element-id="sqmc" style="width: 70%" />
            </FormItem>
          </Col>
          <Col span="24" class="mr50">
            <FormItem label="楼号：" prop="xqlouhao" label-for="xqlouhao">
              <Input v-model="formValidate.xqlouhao" placeholder="请输入楼号" element-id="xqlouhao"
                     style="width: 70%" />
            </FormItem>
          </Col>
          <Col span="24" class="mr50">
            <FormItem label="楼位置：" prop="dylimg" label-for="dylimg">
              <Input v-model="formValidate.dylimg" placeholder="请输入楼位置" element-id="dylimg" style="width: 70%" />
            </FormItem>
          </Col>
          <Col span="24" class="mr50">
            <FormItem label="楼说明：" prop="lmsg" label-for="lmsg">
              <Input v-model="formValidate.lmsg" type="textarea" placeholder="请输入楼说明" style="width: 70%" />
            </FormItem>
          </Col>
        </Row>
        <FormItem>
          <Button type="primary" class="submission" @click="onsubmit('formValidate')">提交</Button>
          <Button type="primary" class="submission" @click="$router.go(-1)">返回</Button>
        </FormItem>
      </Form>
    </Card>
  </div>
</template>

<script>
import { mapState } from "vuex";
import uploadPictures from "@/components/uploadPictures";
import WangEditor from "@/components/wangEditor/index.vue";
import { cmsAddApi, createApi, categoryTreeListApi } from "@/api/cms";
import { editBuildingDetailApi, getBuildingDetailApi } from "@/api/building";

export default {
  name: "addBasicInfoBuilding",
  components: { uploadPictures, WangEditor },
  data() {
    const validateUpload = (rule, value, callback) => {
      if (this.formValidate.image_input) {
        callback();
      } else {
        callback(new Error("请上传图文封面"));
      }
    };
    const validateExplorationCompany = (rule, value, callback) => {
      if (!this.formValidate.cid) {
        callback(new Error("请选择勘探公司"));
      } else {
        callback();
      }
    };
    return {
      dialog: {},
      isChoice: "单选",
      grid: {
        xl: 8,
        lg: 8,
        md: 12,
        sm: 24,
        xs: 24
      },
      gridPic: {
        xl: 6,
        lg: 8,
        md: 12,
        sm: 12,
        xs: 12
      },
      gridBtn: {
        xl: 4,
        lg: 8,
        md: 8,
        sm: 8,
        xs: 8
      },
      loading: false,
      formValidate: {
        "accountNumber": "",
        "asysAclList": [
          {
            "aclModuleId": 0,
            "code": "",
            "id": 0,
            "listAsysAcl": [
              {
                "aclModuleId": 0,
                "code": "",
                "id": 0,
                "listAsysAcl": [
                  {}
                ],
                "name": "",
                "operateIp": "",
                "operateTime": "",
                "operator": "",
                "remark": "",
                "seq": 0,
                "status": 0,
                "type": 0,
                "url": ""
              }
            ],
            "name": "",
            "operateIp": "",
            "operateTime": "",
            "operator": "",
            "remark": "",
            "seq": 0,
            "status": 0,
            "type": 0,
            "url": ""
          }
        ],
        "asysRoleList": [
          {
            "asysAcllistid": "",
            "id": 0,
            "name": "",
            "operateIp": "",
            "operateTime": "",
            "remark": "",
            "shuoming": "",
            "status": 0,
            "type": 0
          }
        ],
        "createTime": "",
        "createUser": "",
        "dizhi": "",
        "dwname": "",
        "gsIds": [],
        "gxfw": "",
        "id": 0,
        "ids": "",
        "img": "",
        "kcfw": "",
        "kcgsid": 0,
        "kcjl": 0,
        "kcly": 0,
        "name": "",
        "phone": "",
        "pwd": "",
        "roleId": 0,
        "roleStr": [],
        "roleStrName": "",
        "scjd": "",
        "sidstate": 0,
        "sqmc": "",
        "state": 0,
        "stateName": "",
        "stateis": "",
        "token": "",
        "updateTime": "",
        "updateUser": "",
        "zjhao": ""
      },
      content: "",
      ruleValidate: {
        jiedaoName: [{ required: true, message: "请输入所属街道", trigger: "blur" }],
        sqmc: [{ required: true, message: "请输入社区名称", trigger: "blur" }],
        xqlouhao: [{ required: true, message: "请输入楼号", trigger: "blur" }],
        dylimg: [{ required: true, message: "请输入楼地址", trigger: "blur" }],
        lmsg: [{ required: true, message: "请输入楼说明", trigger: "blur" }]
      },
      value: "",
      modalPic: false,
      template: false,
      treeData: [],
      formValidate2: {
        type: 1
      },
      myConfig: {
        autoHeightEnabled: false, // 编辑器不自动被内容撑高
        initialFrameHeight: 500, // 初始容器高度
        initialFrameWidth: "100%", // 初始容器宽度
        UEDITOR_HOME_URL: "/admin/UEditor/",
        serverUrl: ""
      }
    };
  },
  computed: {
    ...mapState("media", ["isMobile"]),
    labelWidth() {
      return this.isMobile ? undefined : 120;
    },
    labelPosition() {
      return this.isMobile ? "top" : "right";
    }
  },
  watch: {
    $route(to, from) {
      if (this.$route.params.id) {
        this.getDetails();
      } else {
        this.formValidate = {
          id: 0,
          title: "",
          author: "",
          image_input: "",
          content: "",
          synopsis: "",
          url: "",
          is_hot: 0,
          is_banner: 0
        };
      }
    }
  },
  mounted() {
    if (this.$route.params.id) {
      this.getDetails();
    }
  },
  created() {
    // this.getClass();
  },
  methods: {
    getEditorContent(data) {
      this.content = data;
    },
    // 选择图片
    modalPicTap() {
      this.modalPic = true;
    },
    // 选中图片
    getPic(pc) {
      this.formValidate.image_input = pc.att_dir;
      this.modalPic = false;
    },
    // 分类
    getClass() {
      categoryTreeListApi()
        .then(async (res) => {
          this.treeData = res.data;
        })
        .catch((res) => {
          this.$Message.error(res.msg);
        });
    },
    // 提交数据
    onsubmit(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          editBuildingDetailApi(this.formValidate, this.$route.params.id ? "PUT" : "POST")
            .then(async (res) => {
              this.$Message.success("操作成功");
              setTimeout(() => {
                this.$router.push({ path: "/admin/basic/information/building" });
              }, 500);
            })
            .catch((res) => {
              this.$Message.error("操作失败");
            });
        } else {
          return false;
        }
      });
    },
    // 楼宇详情
    getDetails() {
      getBuildingDetailApi(this.$route.params.id ? this.$route.params.id : 0)
        .then(async (res) => {
          this.formValidate = res.data;
          console.log(this.formValidate);
        })
        .catch((res) => {
          this.loading = false;
          this.$Message.error("操作成功");
        });
    },
    addExplorationCompany() {

    }
  }
};
</script>
<style scoped>
/deep/ .ivu-form-item-content {
    line-height: unset !important;
}

.picBox {
    display: inline-block;
    cursor: pointer;
}

.form .goodsTitle {
    border-bottom: 1px solid rgba(0, 0, 0, 0.09);
    margin-bottom: 25px;
}

.form .goodsTitle ~ .goodsTitle {
    margin-top: 20px;
}

.form .goodsTitle .title {
    border-bottom: 2px solid #1890ff;
    padding: 0 8px 12px 5px;
    color: #000;
    font-size: 14px;
}

.form .goodsTitle .icons {
    font-size: 15px;
    margin-right: 8px;
    color: #999;
}

.form .add {
    font-size: 12px;
    color: #1890ff;
    padding: 0 12px;
    cursor: pointer;
}

.form .radio {
    margin-right: 20px;
}

.form .submission {
    width: 10%;
    margin-left: 27px;
}

.form .upLoad {
    width: 58px;
    height: 58px;
    line-height: 58px;
    border: 1px dotted rgba(0, 0, 0, 0.1);
    border-radius: 4px;
    background: rgba(0, 0, 0, 0.02);
}

.form .iconfont {
    color: #898989;
}

.form .pictrue {
    width: 60px;
    height: 60px;
    border: 1px dotted rgba(0, 0, 0, 0.1);
    margin-right: 10px;
}

.form .pictrue img {
    width: 100%;
    height: 100%;
}

.Modals .address {
    width: 90%;
}

.Modals .address .iconfont {
    font-size: 20px;
}

.tip {
    margin-top: 10px;
    color: #bbb;
}

.addfont {
    display: inline-block;
    font-size: 13px;
    font-weight: 400;
    color: #1890FF;
    margin-left: 14px;
    cursor: pointer;
}
</style>
